﻿var bs = 1;
function TryReg()
{
  	var yhm=document.getElementById('yhm');
  	var sjh=document.getElementById('sjh');
  	var pw=document.getElementById('pw');
  	var pw2=document.getElementById('pw2');
	var aI=document.getElementsByTagName('i')[0];
    aI.innerHTML = "";
    if(bs == 0){
        bs = 1;
        aI.style.color = "red";
    }
    else {
        bs = 0;
        aI.style.color = "white";
    }
    
    if(yhm.value == ""){
        aI.innerHTML='<br />用户名 不可为空';
        return false;
    }else if(sjh.value == ""){
        aI.innerHTML='<br />手机号 不可为空';
        return false;
    }else if(sjh.value.length != 11){
        aI.innerHTML='<br />手机号 不正确<br />';
        return false;
    }else if(pw.value == ""){
        aI.innerHTML='<br />密码不可为空';
        return false;
    }else if(pw2.value == ""){
        aI.innerHTML='<br />再输入一次密码';
        return false;
    }else if(yhm.value.length > 20){
        aI.innerHTML='<br />用户名太长';
        return false;
    }else if(pw2.value != pw.value){
        aI.innerHTML='<br />两次密码不一致';
        return false;
    }
    aI.innerHTML='<br />注册中……';
	var postData = {
	    "yhm": yhm.value,
	    "sjh": sjh.value,
	    "pw": pw.value
		};
        
	postData = (function(obj){ // 转成post需要的字符串.
	    var str = "";
 
	    for(var prop in obj){
     	  	 str += prop + "=" + obj[prop] + "&"
    	}
   	 return str;
	})(postData);
 	
	var xhr = new XMLHttpRequest();
 
	xhr.open("POST", "register.php?m="+Math.random().toString(), true);
	xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xhr.onreadystatechange = function(){
 	   var XMLHttpReq = xhr;
  	  if (XMLHttpReq.readyState == 4) {
   	     if (XMLHttpReq.status == 200) {
    	        var text = XMLHttpReq.responseText;
		          if(parseInt(text) == 1)
		          {
			         window.location.href = "./";
			         return false;
		          }
		          aI.innerHTML = "<br />" + text;
    	    }
   	 }	
	};
	xhr.send(postData);
	return false;
}